using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;

public class PlayerBehavior : MonoBehaviour
{
    public float moveAcceleration = .05f;
    public float moveMaxSpeed = 2f;

    public int totalShotgunBullets = 5;
    public float totalShotgunAngle = 60f;
    public float shotgunCooldown = .5f;
    private float currentShotgunAngle = 60f;
    private float currentShotgunCooldown = 0f;


    public float dashCooldown = 1f;
    public float dashTime = .5f;
    public float dashMoveAcceleration = 3f;
    public float dashMaxMoveSpeed = 5f;
    private bool dashing = false;
    private float currentDashTime;
    private float currentDashCooldown;

    private void Update()
    {
        if (dashing)
            rigidbody2D.mass = 100;
        else
            rigidbody2D.mass = 10;

        if (!dashing)
        {
            var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            var angle = MathHelper.GetAngle(this.transform.position, mousePos) * Mathf.Rad2Deg;
            var quat = Quaternion.AngleAxis(angle, Vector3.forward);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, quat, 100);
        }

        if (Input.GetButton("Fire1"))
        {
            if (currentShotgunAngle > 10f)
                currentShotgunAngle -= 1f;

            Debug.DrawLine(transform.position, new Vector2(transform.position.x, transform.position.y) + MathHelper.VectorFromAngle((rigidbody2D.rotation - currentShotgunAngle / 2f) * (Mathf.PI / 180f)) * 0.5f);
            Debug.DrawLine(transform.position, new Vector2(transform.position.x, transform.position.y) + MathHelper.VectorFromAngle((rigidbody2D.rotation + currentShotgunAngle / 2f) * (Mathf.PI / 180f)) * 0.5f);
        }

        if (Input.GetButtonUp("Fire1") && currentShotgunCooldown == 0f)
        {
            for (int i = 0; i < totalShotgunBullets; i++)
                this.FireShotgunBullet();

            currentShotgunCooldown = shotgunCooldown;
        }

        if (dashing)
        {
            currentDashTime += Time.deltaTime;
            if (currentDashTime >= dashTime)
                dashing = false;
        }

        if (Input.GetButtonUp("Fire2") && !dashing && currentDashCooldown == 0)
            this.Dash();

        if (!Input.GetButton("Fire1"))
            currentShotgunAngle = totalShotgunAngle;

        if (currentShotgunCooldown > 0f)
            currentShotgunCooldown -= Math.Min(currentShotgunCooldown, Time.deltaTime);

        if (currentDashCooldown > 0f)
            currentDashCooldown -= Math.Min(currentDashCooldown, Time.deltaTime);
    }

    private void FixedUpdate()
    {
        var horiz = Input.GetAxis("Horizontal");
        var vert = Input.GetAxis("Vertical");

        if (dashing)
        {
            var v = MathHelper.VectorFromAngle(rigidbody2D.rotation * Mathf.Deg2Rad);
            var deltaVelocity = v * dashMoveAcceleration;

            var newVelocity = rigidbody2D.velocity + deltaVelocity;
            if (newVelocity.magnitude > dashMaxMoveSpeed)
                newVelocity *= dashMaxMoveSpeed / newVelocity.magnitude;

            rigidbody2D.velocity = newVelocity;
        }
        else if (horiz != 0 || vert != 0)
        {
            var v = new Vector2(horiz, vert);
            var deltaVelocity = v * moveAcceleration;

            var newVelocity = rigidbody2D.velocity + deltaVelocity;
            if (newVelocity.magnitude > moveMaxSpeed)
                newVelocity *= moveMaxSpeed / newVelocity.magnitude;

            rigidbody2D.velocity = newVelocity;
        }

        //Debug.Log(rigidbody2D.velocity)
    }

    private void FireShotgunBullet()
    {
        var min = rigidbody2D.rotation - currentShotgunAngle / 2f;
        var max = rigidbody2D.rotation + currentShotgunAngle / 2f;

        var randomAngle = UnityEngine.Random.Range(min, max);
        var bullet = Instantiate(_bullet, rigidbody2D.position, Quaternion.identity) as Rigidbody2D;

        var b = bullet.GetComponent<ProjectileBehavior>();
        b.Shooter = gameObject;
        b.Damage = 0.3f;
        b.SecondsAlive = 1f;

        bullet.rotation = randomAngle;
        bullet.velocity = MathHelper.VectorFromAngle((float)(bullet.rotation * (Mathf.PI / 180d))) * UnityEngine.Random.Range(3f, 5f);
    }

    private void Dash()
    {
        dashing = true;
        //rigidbody2D.velocity += MathHelper.VectorFromAngle (rigidbody2D.rotation * Mathf.Deg2Rad) * 3f;


        currentDashCooldown = dashCooldown;
        currentDashTime = 0f;
    }

    public Rigidbody2D _bullet;
}

public static class MathHelper
{
    public static Vector2 VectorFromAngle(float radians)
    {
        return new Vector2((float)Mathf.Cos(radians), (float)Mathf.Sin(radians));
    }

    public static float GetAngle(Vector2 p1, Vector2 p2)
    {
        float xDiff = p2.x - p1.x;
        float yDiff = p2.y - p1.y;
        return (float)(Mathf.Atan2(yDiff, xDiff));// * (180 / Math.PI));
    }
}